<template>
    <Modal :mask-closable="false"
            v-model="resultVisble"
            class="modal-content-layout"
            :title="`${$t('ledger.Results')}`"
    >
        <Form ref="resultAcceptCheckForm" :model="formTop" label-position="top">
              <FormItem :label="`${$t('ledger.Results')}:`" required>
                <RadioGroup v-model="formTop.arrivalResult">
                    <Radio v-for="item in resultList" :label="item.id" :key="item.id">{{ item.name }}</Radio>
                </RadioGroup>
                <Input v-model="formTop.arrivalDesc" type="textarea" :rows="4" :placeholder="$t('preliminary.p6')"/>
            </FormItem>
          <FormItem :label="`${$t('ledger.DeviceAdministrator')}:`">
                <Input v-model="formTop.belongCompanyAdminName" disabled/>
            </FormItem>
          <FormItem :label="`${$t('ledger.Companyacceptancepersonnel')}:`">
                <Input v-model="formTop.belongCompanyAcceptorName" disabled/>
            </FormItem>
          <FormItem :label="`${$t('ledger.Usecompanyacceptancepersonnel')}:`">
                <Input v-model="formTop.useCompanyAcceptorName" disabled/>
            </FormItem>
          <FormItem :label="`${$t('ledger.Factoryaffairscenteracceptancepersonnel')}:`">
                <Input v-model="formTop.arrivalFacAcceptorName" disabled/>
            </FormItem>
          <FormItem :label="`${$t('preliminary.Buyer')}:`">
                <Input v-model="formTop.buyer" disabled/>
            </FormItem>
          <FormItem :label="`${$t('preliminary.Upload')}:`" required>
                <Upload
                        ref="fileUpload"
                        :show-upload-list="false"
                        :before-upload="handleBeforeUpload"
                        type="drag"
                        action=""
                        style="width: 70px"
                >
                    <Button type="primary">{{$t('preliminary.Upload')}}</Button>
                </Upload>
                <div class="file-list" v-for="(item, index) in setFileList" :key="index" >
                    <div class="item-src">{{ item.fileName }}</div>
                    <Icon class="item-icon" type="ios-close" size="20" style="float:right;" @click="delFileList(index)" v-if="item"></Icon>
                </div>
            </FormItem>
        </Form>
        <div slot="footer">
            <Button type="default" @click="handleCancel">{{$t('common.Cancle')}}</Button>
            <Button type="primary" :loading="loadingVisible" @click="handleOk">{{$t('maintenance.Confirmed')}}</Button>
        </div>
    </Modal>
</template>

<script>
    export default {
        name: "resultAcceptanceCheckModal",
        data() {
            return {
                resultVisble: false,
                loadingVisible: false,
                formTop: {},
                resultList: [{id: 1, name: this.$t('acceptanceCheck.inAcceptancePassed')}, {id: 2, name: this.$t('acceptanceCheck.inAcceptanceFailed')}],
                setFileList: []
            }
        },
        methods: {
            show(record) {
                this.formTop = { ...record }
                this.setFileList = []
                this.resultVisble = true

            },
            //文件上传
            handleBeforeUpload(file) {
                let formData = new FormData();
                formData.append("dir", 'device')
                formData.append("file",file)
                let config = {
                    headers: {
                        "Content-Type": "multipart/form-data"
                    }
                };
                this.axios({
                    method: 'post',
                    url: '/fileServer/upload',
                    data: formData,
                    config,
                }).then(res => {
                    if (res.code == 200) {
                        this.setFileList.push({
                            fileName: res.data.fileName,
                            filePath: res.data.filepath,
                            fileUrl: res.data.fileUrl
                        })
                    }
                })
                return false;
            },
            //文件预览
            handlePreview(item) {
                if (item) {
                    window.open(this.util.filePathToUrl(item.filePath))
                }
            },
            //文件删除
            delFileList(id) {
                this.setFileList = this.setFileList.filter((item, index) => index !=id)
            },
            handleOk() {
                if (!this.formTop.belongCompanyAdminName) {
                    this.$Message.warning(({content: this.$t('acceptanceCheck.noSave'),duration:6,closable:true}));
                    return false
                }
                if (!this.setFileList.length || !this.formTop.arrivalResult) {
                    this.$Message.warning(({content: this.$t('acceptanceCheck.inputAllInfo'),duration:6,closable:true}));
                    return false
                }
                if(this.formTop.arrivalResult == 2 && !this.formTop.arrivalDesc) {
                    this.$Message.warning(({content:this.$t('acceptanceCheck.noPassReason'),duration:6,closable:true}));
                    return false
                }
                this.loadingVisible = true
                let dmsAttachmentList = this.setFileList.map(item => {
                    return {
                        ...item,
                        attachmentType: 15
                    }
                })
                let params = {
                    dmsAttachmentList,
                    deviceMId: this.formTop.deviceMId,
                    arrivalResult: this.formTop.arrivalResult,
                    arrivalDesc: this.formTop.arrivalDesc,
                    belongCompanyAdminName: this.formTop.belongCompanyAdminName,
                    belongCompanyAdminNo: this.formTop.belongCompanyAdminNo,
                    arrivalDeptAcceptorName: this.formTop.arrivalDeptAcceptorName,
                    arrivalDeptAcceptorNo: this.formTop.arrivalDeptAcceptorNo,
                    arrivalFacAcceptorName: this.formTop.arrivalFacAcceptorName,
                    arrivalFacAcceptorNo: this.formTop.arrivalFacAcceptorNo,
                    buyer: this.formTop.buyer
                }
                this.axios({
                    method: 'post',
                    url: '/dms/dmsArrivalCheck/add',
                    data: params
                }).then(res => {
                    if(res.code === 200){
                        this.$emit('handleFresh')
                        this.resultVisble = false
                        this.$refs.resultAcceptCheckForm.resetFields();
                        this.$Message.success(({content:this.$t('acceptanceCheck.saveSuccess'),duration:3,closable:true}));
                    }else{
                        this.$Message.warning(({content:res.msg,duration:6,closable:true}));
                    }
                }).finally(() => {
                    this.loadingVisible = false
                })
            },
            handleCancel() {
                this.resultVisble = false
            }
        }
    }
</script>

<style scoped lang="scss">
    .file-list {
        margin: 0 20px;
        .item-src {
            width: 80%;
            display: inline-block;
            line-height: 26px;
            color: #187bdb;
            cursor: pointer;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
        }
        .item-icon {
            width: 20%;
            display: inline-block;
            margin-top: 4px;
        }
    }
</style>
